package company.maestro;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import au.com.bytecode.opencsv.CSVReader;

public class CsvImportActivity extends Activity
{
	private static final int REQUEST_PICK_FILE = 1;
	
	Button btnChooser;
	TextView lblCsvResults;
	
	public static StudentDBAdapter 	studentDBAdapter;
	
	@Override
    public void onCreate(Bundle icicle) 
	{
    	super.onCreate(icicle);

        setContentView(R.layout.csv_import);

        btnChooser = (Button)findViewById(R.id.btnChooser);
        //lblCsvResults = (TextView)findViewById(R.id.lblShowCSVResults);
        btnChooser.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) 
			{
				Intent intent = new Intent(CsvImportActivity.this, FilePickerActivity.class);
				startActivityForResult(intent, REQUEST_PICK_FILE);
			
			}
		});
        
        studentDBAdapter = StudentDBAdapter.getInstance(this);
    }
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if(resultCode == RESULT_OK) {
	switch(requestCode) {
	case REQUEST_PICK_FILE:
	if(data.hasExtra(FilePickerActivity.EXTRA_FILE_PATH)) {
	// Get the file path
	File f = new File(data.getStringExtra(FilePickerActivity.EXTRA_FILE_PATH));
	String fileContents = "";
	studentDBAdapter.open();
	
	try
	{
		CSVReader reader = new CSVReader(new FileReader(f.getPath()), ';');
	    String [] nextLine;
	    reader.readNext();
	    reader.readNext();
	    
	    while ((nextLine = reader.readNext()) != null) 
	    {
	        // nextLine[] is an array of values from the line
	    	
	        
	        
	        String[] studentName 	= nextLine[0].split(",");
	        String firstName 		= studentName[1].trim();
	        String lastName 		= studentName[0].trim();
	        String guardianEmail 	= "";
	        String phone			= "";
	        
	        if ( nextLine.length >= 3)
	        {
	        	guardianEmail 	= nextLine[2];
	        }
	        if ( nextLine.length >= 4 )
	        {
	        	phone 			= nextLine[3];
	        }
	        
	        String guardianInformation = "";
	        
	        if (nextLine.length >= 7 )
	        {
	        	guardianInformation = nextLine[6];
	        }
	        
	        
	        String[] guardianParts = null;
	        
	        if ( guardianInformation.split("\n") != null )
	        {
	        	guardianParts = guardianInformation.split("\n");
	        }
	        
	        String guardianName = "";
	        String guardianPhone = phone;
	        
	        if ( guardianParts != null && guardianParts[0] != "" )
	        {
	        	guardianName = guardianParts[0];
	        	
//	        	if ( guardianParts[1] != "" )
//		        {
//		        	guardianPhone = guardianParts[1];
//		        }
	        }
	        
	        
	        fileContents += "Guardian name: " + guardianName + ", Guardian phone: " + guardianPhone + "\n";
	        
	        Student studentToAdd 	= new Student(firstName, lastName);
	        studentToAdd.setGuardianName(guardianName);
	        studentToAdd.setGuardianEmail(guardianEmail);
	        studentToAdd.setGuardianPhone(guardianPhone);
	        
	        // Add each student to the database
	        studentDBAdapter.insertStudent(studentToAdd);
	        
	    }
	    reader.close();
	    studentDBAdapter.close();
	}
	catch (IOException e)
	{
		fileContents += "An error has occurred.";
	}
	
	fileContents += "The contents of this file have been added to the database.";

	// Set the file path text view
	//lblCsvResults.setText(fileContents);
	}
	}
	}
	}
	
}
